﻿#region Copyright

// The contents of this file are subject to the Mozilla Public License
//  Version 1.1 (the "License"); you may not use this file except in compliance
//  with the License. You may obtain a copy of the License at
//  
//  http://www.mozilla.org/MPL/
//  
//  Software distributed under the License is distributed on an "AS IS"
//  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
//  License for the specific language governing rights and limitations under 
//  the License.
//  
//  The Initial Developer of the Original Code is Robert Smyth.
//  Portions created by Robert Smyth are Copyright (C) 2008.
//  
//  All Rights Reserved.

#endregion

using System;
using NUnitGrid.Framework.Logging;
using NUnitGrid.Framework.NUnitGrid;
using NUnitGrid.Framework.Pipes;


namespace NUnitGrid.Protocol.Commands
{
    public class RunAsProcessTransaction : IProtocolTransaction
    {
        private readonly string clientMachineName;
        private readonly string commandLine;
        private readonly string enviromentVariable;
        private readonly string enviromentVariableValue;
        private readonly string fileName;
        private readonly IPipe link;
        private readonly IGridTaskTransactionListener listener;
        private readonly ILogger logger;
        private readonly TimeSpan timeLimit;
        private readonly string workingDirectory;
        private ProtocolAppCommandMessage commandMessage;

        public RunAsProcessTransaction(IPipe link, ILogger logger, IGridTaskTransactionListener listener,
                                       string clientMachineName, string workingDirectory, string fileName,
                                       string commandLine, TimeSpan timeLimit, string enviromentVariable,
                                       string enviromentVariableValue)
        {
            this.link = link;
            this.logger = logger;
            this.listener = listener;
            this.clientMachineName = clientMachineName;
            this.workingDirectory = workingDirectory;
            this.fileName = fileName;
            this.commandLine = commandLine;
            this.timeLimit = timeLimit;
            this.enviromentVariable = enviromentVariable;
            this.enviromentVariableValue = enviromentVariableValue;
        }

        public void SendCommand()
        {
            logger.Info.Write("Starting run as process transaction");

            commandMessage = new ProtocolAppCommandMessage("RunOnGrid.AsProcess");

            commandMessage.AddProperty("WorkingDirectory", workingDirectory);
            commandMessage.AddProperty("FileName", fileName);
            commandMessage.AddProperty("CommandLine", commandLine);
            commandMessage.AddProperty("TimeLimit", timeLimit);
            commandMessage.AddProperty("ClientMachineName", clientMachineName);
            commandMessage.AddProperty("EnvironmentVariables",
                                       string.Format("\n{0}={1}\n", enviromentVariable, enviromentVariableValue));

            listener.OnTaskStarted(this);

            link.SendMessage(commandMessage.ToString());
        }

        public bool IsValidReponse(string responseMessage)
        {
            return commandMessage.IsResponseMessage(responseMessage);
        }

        public void ParseResponse(string response)
        {
            logger.Info.Write("Completing run as process transaction");

            var parser = new ProtocolMessageParser(response);
            var succeeded = bool.Parse(parser.GetValue("Succeeded"));
            var textOutput = parser.GetValue("TextOutput");
            var machineName = parser.GetValue("MachineName");

            listener.OnTaskCompleted(this, succeeded, textOutput, machineName);
        }
    }
}